var tableIns;//表格实例
layui.use(['laydate','form','table'], function(){
    var form = layui.form;
    var laydate = layui.laydate;
    var table = layui.table;
    laydate.render({
		elem: '#startMonth' //指定元素
		,type: 'month'
	});
    laydate.render({
		elem: '#endMonth' //指定元素
		,type: 'month'
	});
    dict2Select("recommendSource","source");
    form.render();
    io.get("/engine/model-nos",function(res){
    	var data = res.data;
    	var m = new Map();
    	for(var i in data){
    		var v= data[i];
    		m.set(String(v),String(v));
    	}
    	map2Select(m,"modelNo");
        form.render();
    });
    tableIns = table.render({
        elem: '#listTable'
        , cols: [[ //表头
            {field: 'modelNo', title: '模型编号', align: 'center',width: 200},
            {field: 'statMonth', title: '统计月份', align: 'center',width: 100},
            {field: 'industry', title: '行业', align: 'center',width: 140},
            {field: 'dangerLine', title: '止损线', align: 'center',width: 100},
            {field: 'sellLine', title: '止盈线', align: 'center',width: 100},
            {field: 'winCount', title: '赢数', align: 'center',width: 100},
            {field: 'loseCount', title: '亏数', align: 'center',width: 100},
            {field: 'pem', title: '盈利', align: 'center',width: 120,templet:function (d) {
            	if(d.pem) {
					return '<b>'+numberFormat(d.pem, 2)+"</b>";
				}else{
            		return '0';
				}
			}}
        ]]
    	, page: true
        , request: {
            pageName: 'pageNo' //页码的参数名称，默认：page
            , limitName: 'pageSize' //每页数据量的参数名，默认：limit
        }
        , parseData: function (res) { //res 即为原始返回的数据
            return {
                "code": res.code, //解析接口状态
                "msg": res.msg, //解析提示文本
                "count": res.data.total, //解析数据长度
                "data": res.data.list //解析数据列表
                ,"attr":res.attr
            };
        }
    });
    
    form.on('submit(search)', function(formData){
    	initBi(formData.field);
        tableIns.reload({
        	url:appendCtx("/bi/hlu/rec-pem"),
            where:formData.field,
            page:{
                curr: 1
            }
        });
        return false;
    });
});

function initBi(data){
    //渲染柱状图
    var summary = echarts.init(document.getElementById('summary'),'macarons');
	summary.showLoading();
	var summaryOption={
			    title: {
	                text: '交易盈亏数据汇总',
	                subtext: '单位（元）',
	                left: 'left'
	            },
			    tooltip : {
			        trigger: 'axis',
			        axisPointer : {            // 坐标轴指示器，坐标轴触发有效
			            type : 'shadow'        // 默认为直线，可选为：'line' | 'shadow'
			        }
			    },
			    grid: {
			        left: '3%',
			        right: '4%',
			        bottom: '3%',
			        containLabel: true
			    },
			    yAxis: {
			        type: 'value'
			    }
	};
	io.get("/bi/hlu/rec-pem/summary",function(res){
		summary.hideLoading();
		summaryOption.xAxis= [{type : 'category',data : res.attr.xAxisData}];
		summaryOption.series=[{type : 'bar',label: {show: true},data : res.attr.data}];
		summary.setOption(summaryOption);
		$('#totalAmount').html(res.attr.sum);
	},data);
}

function train(){
	layer.open({
        type: 2,
        title: '模型训练',
        area: ['500px', '500px'],
        content: 'train.html',
    });
}

function perm(){
	layer.open({
        type: 2,
        title: '表现统计',
        area: ['500px', '500px'],
        content: 'pem.html',
    });
}